// pages/game/game.js
const app = getApp()
const dayjs = app.dayjs
const login_js = require('../../utils/login.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    inner_idx: 0,
    user_data: {},
    is_master: false,
    title: '',
    pointer: 0, //计数器
    total: 0,
    model_idx: 0,
    model: ['+', '-', '×', '÷'],
    answer: null,
    last_number: '',
    number_pick: '',
    star_time: null,
    score_number: null,
    has_dot: false,
    is_wrong: false,
    is_dark: false,
    is_time: false, //保存时间
    modal_show: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id
    this.setData({
      user_data: app.global_data.user_data,
      id,
      star_time: dayjs()
    })
    this.getTest()
  },
  ...login_js.methods,
  /**
   * 登录时候获取用户信息回调
   */
  async getUserInfo(e) {
    await this.getProfile()
    this.clickModalCancel()
  },
  clickModalCancel() {
    this.setData({
      modal_show: false,
    })
  },
  /* 获取题库 */
  getTest() {
    wx.showLoading({
      title: '题目加载中',
    })
    wx.cloud.callFunction({
      name: 'runDb',
      data: {
        type: 'get_detail',
        collection: 'test',
        is_where: {
          "_id": this.data.id,
        }
      },
      complete: res => {
        let detail = res.result.data[0]
        // console.log(detail)
        let list = detail.test
        list.forEach(i => {
          i.math_model = this.data.model[i.model_idx]
        })
        wx.hideLoading()
        // console.log(list)
        this.setData({
          is_master: detail.master_openid === this.data.user_data.openid,
          title: detail.title,
          test_list: list,
          total: list.length
        })
        wx.setStorageSync('score_model', detail.score_model)
        wx.setNavigationBarTitle({
          title: detail.title,
        })
        this.setTest()
      }
    })
  },
  /* 设置题目 */
  setTest() {
    if (this.data.pointer < this.data.total) {
      this.setData({
        number_pick: '',
        first_bit: this.data.test_list[this.data.pointer].fb,
        second_bit: this.data.test_list[this.data.pointer].sb,
        math_model: this.data.test_list[this.data.pointer].math_model,
        answer: this.data.test_list[this.data.pointer].answer,
        pointer: this.data.pointer + 1
      })
    } else {
      this.setScore()
    }
  },
  /* 数字输入 */
  numberPick(e) {
    //检测数字长度
    if (this.data.number_pick.length > 10) {
      wx.vibrateLong({
        success: (res) => {},
      })
      return
    }
    let {
      idx,
    } = e.currentTarget.dataset
    // 检测小数点输入
    if (idx == '.') {
      if (this.data.has_dot) {
        wx.vibrateLong({
          success: (res) => {},
        })
        return
      } else {
        this.setData({
          has_dot: true
        })
      }
    }
    this.setData({
      last_number: idx,
      number_pick: this.data.number_pick + idx
    })
  },
  /* 退格 */
  backSpace() {
    if (this.data.last_number == '.') {
      this.setData({
        has_dot: false
      })
    }
    let number = this.data.number_pick
    let is_length = number.length
    number = number.slice(0, is_length - 1)
    let is_last = ''
    if (is_length > 1) {
      is_last = number.slice(is_length - 2, is_length - 1)
    } else {
      is_last = number
    }
    this.setData({
      last_number: is_last,
      number_pick: number
    })
  },
  /* 清空输入 */
  clearNumber() {
    this.setData({
      has_dot: false,
      number_pick: ''
    })
  },
  /* 确认答案 */
  confirmAnswer() {
    let has_answer = +this.data.number_pick
    if (has_answer === this.data.answer) {
      this.setData({
        is_wrong: false,
      })
      this.setTest()
    } else {
      this.setData({
        is_wrong: true
      })
      wx.vibrateLong({
        success: (res) => {},
      })
    }
  },
  /* 获取成绩 */
  setScore() {
    if (!this.data.is_time) {
      let now_time = dayjs()
      let score_number = now_time - this.data.star_time
      this.setData({
        is_time: true,
        score_number
      })
      wx.setStorageSync('user_test', this.data.test_list)
    }
    wx.redirectTo({
      url: `/pages/score/score?title=${this.data.title}&test_count=${this.data.total}&score_number=${this.data.score_number}&test_id=${this.data.id}&is_master=${this.data.is_master}`,
    })
  }
})